

CalendarService = Packages.com.google.gdata.client.calendar.CalendarService;
gdata           = Packages.com.google.gdata.data;
AclEntry        = Packages.com.google.gdata.data.acl.AclEntry;
AclScope        = Packages.com.google.gdata.data.acl.AclScope
CalendarAclRole = Packages.com.google.gdata.data.calendar.CalendarAclRole


function main( ui ) { 

    var json = ui.getQueryParameters().getFirst("json");
    var o = new JSONObject( json );
    var EMAIL_JSON_KEY = "EMAIL";
    var ID_CALENDAR_JSON_KEY = "IDCALENDAR";
    
    var service = new CalendarService("demo-calendar-1");
    
    var username = "soulsoftware2010@gmail.com";
    var password = "2010soulsoftware";
    var idCalendar = o.getString(ID_CALENDAR_JSON_KEY);
    var partecipant = o.getString(EMAIL_JSON_KEY);
  
    service.setUserCredentials(username, password);
    try {

      
         var entry = new AclEntry();
         entry.setScope(new AclScope(AclScope.Type.USER, partecipant));
         entry.setRole(CalendarAclRole.READ);
 
         var aclUrl =
           new java.net.URL("https://www.google.com/calendar/feeds/"+idCalendar+"/acl/full");
 
         var result = service.insert(aclUrl, entry);
 

    }
    catch( e ) {
    
        return e;
    }
    
    return "OK";

}